package com.suixinsanren.design.state.demo.demo04;

public class SoldState implements State {

    private final GumballMachine gumballMachine;

    public SoldState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("现在无法投币，我们正在为您准备糖果");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("游戏已开始，无法退还硬币，我们正在为您准备糖果");
    }

    @Override
    public void turnCrank() {
        System.out.println("请不要重复转动曲柄，我们正在为您准备糖果");
    }

    @Override
    public void dispense() {
        if (gumballMachine.getCount() > 0) {
            gumballMachine.releaseBall();
            System.out.println("您获得了1颗糖果");
            if (gumballMachine.getCount() > 0) {
                gumballMachine.setCurrentState(gumballMachine.getNoQuarterState());
            }
            return;
        }
        System.out.println("抱歉，糖果已经售罄，已退还您的硬币");
        gumballMachine.setCurrentState(gumballMachine.getSoldOutState());
    }

    @Override
    public String toString() {
        return "正在准备糖果状态";
    }
}
